package com.baihuabooks.scoresys.servlets;

import com.alibaba.fastjson.JSON;
import com.baihuabooks.scoresys.common.DBUtil;
import com.baihuabooks.scoresys.common.Locale;
import com.baihuabooks.scoresys.entity.Customer;
import com.baihuabooks.scoresys.entity.ExchangeStrategy;
import com.baihuabooks.scoresys.entity.Response;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2016/5/15 0015.
 */
public class ExchangeStrategyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<ExchangeStrategy> exchangeStrategies = selectAll();

        String json = JSON.toJSONString(exchangeStrategies);
        resp.getWriter().print(json);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String score = req.getParameter("score");
        String rmb = req.getParameter("rmb");

        Response response = new Response();
        if(StringUtils.isBlank(score)) {
            response.setCode(1);
            response.setDesc(Locale.get("strategy_error_msg_001"));
        } else if(StringUtils.isBlank(rmb)) {
            response.setCode(1);
            response.setDesc(Locale.get("strategy_error_msg_002"));
        } else {

            int _score = Integer.valueOf(score);
            BigDecimal _rmb = BigDecimal.valueOf(Double.valueOf(rmb));
            try {
                insert(_score, _rmb);
            } catch (SQLException e) {
                e.printStackTrace();
                response.setCode(1);
                response.setDesc(Locale.get("strategy_error_msg_003"));
            }
        }
        resp.getWriter().print(response);
    }

    private List<ExchangeStrategy> selectAll() {

        String sql = " SELECT id,score,rmb,date_format(time, '%Y-%m-%d %H:%i:%s') as time " +
                        "FROM EXCHANGE_STRATEGY WHERE del_flag=0";

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        List<ExchangeStrategy> exchangeStrategies = new ArrayList<ExchangeStrategy>();

        try {
            conn = DBUtil.getInstance().getConnection();
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                ExchangeStrategy exchangeStrategy = new ExchangeStrategy();
                exchangeStrategy.setId(rs.getInt("id"));
                exchangeStrategy.setScores(rs.getInt("score"));
                exchangeStrategy.setRmb(rs.getBigDecimal("rmb"));
                exchangeStrategy.setTime(rs.getString("time"));
                exchangeStrategies.add(exchangeStrategy);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, stmt, conn);
        }

        return exchangeStrategies;
    }

    private void insert(int score, BigDecimal rmb) throws SQLException {

        String sql = " INSERT INTO EXCHANGE_STRATEGY(SCORE, RMB, TIME) VALUES(?,?,NOW())";

        Connection conn = null;
        PreparedStatement stmt = null;

        List<ExchangeStrategy> exchangeStrategies = new ArrayList<ExchangeStrategy>();

        try {
            conn = DBUtil.getInstance().getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, score);
            stmt.setBigDecimal(2, rmb);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw e;
        } finally {
            DBUtil.close(stmt, conn);
        }
    }
}
